package br.usp.pcs.basys.event;

import java.util.Date;
import java.util.List;

import br.usp.pcs.basys.Job;
import br.usp.pcs.basys.engine.IResources;

public abstract class ComputerSystemEvent implements Event {

	protected final Job job;
	protected Date date;
	protected final IResources resources;

	public ComputerSystemEvent(Job job, Date date, IResources resources) {
		super();
		this.job = job;
		this.date = date;
		this.resources = resources;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public void execute() {
		Action action = new Action(date, this, job);
		setup();
		List<ActionResult> result = run();
		action.addAll(result);
		registerStatistics(action);
	}

	private void registerStatistics(Action action) {
		System.out.println(action);
	}

	abstract List<ActionResult> run();

	private void setup() {
		resources.setSimulationDate(date);

	}

	public Date getDateAfterWaiting(long duration) {
		long currentSimulationTime = resources.getSimulationDate().getTime();
		Date result = new Date(currentSimulationTime + duration);
		return result;
	}

	@Override
	public String toString() {
		return this.getClass().getSimpleName() + "@(" + date.getTime() + ")";
	}

	public int compareTo(Event o) {
		return this.date.compareTo(o.getDate());
	}

}
